package jp.mixi.android.sdk.hackathon.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONObjectUtil {

    public static String getStringByName(JSONObject obj, String name) {

        try {
            if (obj.has(name)) {
                return obj.getString(name);
            } else {
                return null;
            }
        } catch (JSONException e) {
            return null;
        }
    }

    public static int getIntByName(JSONObject obj, String name) {

        try {
            if (obj.has(name)) {
                return obj.getInt(name);
            } else {
                return 0;
            }
        } catch (JSONException e) {
            return 0;
        }
    }

    public static boolean getBooleanByName(JSONObject obj, String name) {
        try {
            if (obj.has(name)) {
                return obj.getBoolean(name);
            } else {
                return false;
            }
        } catch (JSONException e) {
            return false;
        }
    }
    public static JSONObject getObjectByName(JSONObject obj, String name) {
        try {
            if (obj.has(name)) {
                return obj.getJSONObject(name);
            } else {
                return null;
            }
        } catch (JSONException e) {
            return null;
        }
    }

    public static JSONArray getJSONArrayByName(JSONObject obj, String name) {

        try {
            if (obj.has(name)) {
                return obj.getJSONArray(name);
            } else {
                return null;
            }
        } catch (JSONException e) {
            return null;
        }
    }

    public static String getStringByIndex(JSONArray array, int index) {

        try {
            if (array.isNull(index)) {
                return null;
            } else {
                return array.getString(index);
            }
        } catch (JSONException e) {
            return null;
        }
    }

    public static JSONObject getObjectByIndex(JSONArray array, int index) {

        try {
            if (array.isNull(index)) {
                return null;
            } else {
                return array.getJSONObject(index);
            }
        } catch (JSONException e) {
            return null;
        }
    }

    public static <T> T[] getStringArrayByName(JSONObject obj, String name) {

        try {
            if (obj.has(name)) {
                JSONArray json = obj.getJSONArray(name);
                int length = json.length();
                Object[] array = new Object[length];
                for (int i = 0; i < length; i++) {
                    array[i] = (T) json.get(i);
                }
                return (T[]) array;
                // return obj.getJSONArray(name);
            } else {
                return (T[]) new Object[0];
            }
        } catch (JSONException e) {
            return (T[]) new Object[0];
        }
    }

}
